#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
typedef int SLDateType;
typedef struct SListNode
{
	SLDateType date;
	struct SListNode* next;
}SLTNode;

SLTNode* BuySLTNode(SLDateType x)
{
	SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		return NULL;
	}
	newnode->date = x;
	newnode->next = NULL;
	return newnode;
}

void SLTpushback(SLTNode** pphead, SLDateType x)
{
	SLTNode* newnode = BuySLTNode(x);
	if (*pphead == NULL)
	{
		*pphead = newnode;
	}
	else
	{
		SLTNode* tail = *pphead;
		while (tail->next != NULL)
		{
			tail = tail->next;
		}
		tail->next = newnode;
	}
}
void SLTpopback(SLTNode** pphead)
{
	assert(pphead);
	if ((*pphead)->next == NULL)
	{
		free(*pphead);
		*pphead = NULL;
	}
	else
	{
		SLTNode* tail = *pphead;
		while (tail->next->next != NULL)
		{
			tail = tail->next;
		}
		free(tail->next);
		tail->next = NULL;
		/*SLTNode* tail = *pphead;
		SLTNode* prex = NULL;
		while (tail->next != NULL)
		{
			
			tail = tail->next;
			prex = tail;
		}
		free(tail);
		tail = NULL;
		prex->next = NULL;*/
	}
}

void SLTpushfront(SLTNode** pphead, SLDateType x)
{
	SLTNode* newnode = BuySLTNode(x);
	newnode->next = *pphead;
	*pphead = newnode;
}
void SLTpopfront(SLTNode** pphead)
{
	assert(pphead);
	assert(*pphead);

	SLTNode* first = *pphead;
	*pphead = first->next;
	free(first);
	first = NULL;
}
void SLTNodeprint(SLTNode* phead)
{
	assert(phead);
	SLTNode* cur = phead;
	while (cur != NULL)
	{
		printf("%d ", cur->date);
		cur = cur->next;
	}
}
void TestSList1()
{
	SLTNode* plist = NULL;
	SLTpushback(&plist, 1);
	SLTpushback(&plist, 2);
	SLTpushback(&plist, 3);
	SLTpushback(&plist, 4);

	SLTNodeprint(plist);

}
void TestSList2()
{
	SLTNode* plist = NULL;
	SLTpushback(&plist, 2);
	SLTpushback(&plist, 3);
	SLTpushback(&plist, 4);
	SLTpopback(&plist);
	SLTNodeprint(plist);
}
void TestSList3()
{
	SLTNode* plist = NULL;
	SLTpushfront(&plist, 2);
	SLTpushfront(&plist, 3);
	SLTpushback(&plist, 4);
	SLTpushfront(&plist, 2);
	SLTpushfront(&plist, 3);
	SLTpushback(&plist, 4);
	SLTpopback(&plist);
	SLTpopfront(&plist);
	SLTNodeprint(plist);
}
int main()
{
	///TestSList1();
	//TestSList2();
	TestSList3();
	return 0;
}
